package com.keepers.repair.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;

import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.net.LogUtil;
import com.keepers.repair.view.OnIntercomListener;

import java.io.File;

/**
 * auth aboom
 * date 2018/10/28
 */
public class RecordingService extends Service {

    private String             mFilePath;
    private MediaRecorder      mRecorder;
    private long               mStartingTimeMillis;
    private RecBinder          recBinder;
    private OnIntercomListener listener;

    public class RecBinder extends Binder {
        public void setListener(OnIntercomListener onIntercomListener) {
            if (onIntercomListener != null) {
                listener = onIntercomListener;
                startRecording();
            }
        }
    }

    public static void starter(Activity context, ServiceConnection connection, boolean isStart) {
        if (isStart) {
            Intent intent = new Intent(context, RecordingService.class);
            context.bindService(intent, connection, BIND_AUTO_CREATE);
        } else {
            context.unbindService(connection);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        recBinder = new RecBinder();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return recBinder;
    }


//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        Log.e("aa" , "onStartCommand ");
//        boolean isStart = intent.getBooleanExtra(EXTRA, false);
//        if (isStart) {
//            startRecording();
//        } else {
//            if (mRecorder != null) {
//                stopRecording();
//            }
//            stopSelf();
//        }
//        return super.onStartCommand(intent, flags, startId);
//    }

    @Override
    public void onDestroy() {
        stopRecording();
        listener = null;
        recBinder = null;
        stopSelf();
        super.onDestroy();
    }

    public void startRecording() {
        setFileNameAndPath();
        try {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            mRecorder.setOutputFile(mFilePath);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//            mRecorder.setAudioChannels(1);
//            mRecorder.setAudioSamplingRate(44100);
//            mRecorder.setAudioEncodingBitRate(192000);
            mRecorder.prepare();
            mRecorder.start();
            if (listener != null) {
                listener.onStartRecord();
            }
            mStartingTimeMillis = System.currentTimeMillis();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e("prepare() failed");
        }
    }

    public void setFileNameAndPath() {
        File sampleDir = new File(Environment.getExternalStorageDirectory()
                + File.separator + ".repaired/voice/");//录制视频的保存地址
        if (!sampleDir.exists()) {
            sampleDir.mkdirs();
        }
        String mFileName = SpHelper.getLoginUserInfo().UserName + "_" + (System.currentTimeMillis()) + ".amr";
        mFilePath = sampleDir + File.separator + mFileName;
    }

    public void stopRecording() {
        if (mRecorder == null) {
            return;
        }
        mRecorder.stop();
        mRecorder.reset();
        mRecorder.release();
        mRecorder = null;
        long mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis);
        if (mElapsedMillis < 1000) {
            File f = new File(mFilePath);
            if (f.exists()) {
                f.delete();
            }
            if (listener != null) {
                listener.onTimeShort();
            }
        } else {
            if (listener != null) {
                listener.onFinishedRecord(mFilePath, mElapsedMillis);
            }
        }
    }
}
